package com.blobfish.fiction.common.shiro.realm;

import com.blobfish.fiction.common.model.User;
import com.blobfish.fiction.user.UserService;
import com.jfinal.aop.Inject;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * shiroRealm
 * @author huangbo
 */
public class JdbcRealm extends AuthorizingRealm {
    @Inject
    private UserService userService;
    /**
     * 验证用户权限
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        User user = (User)principalCollection.getPrimaryPrincipal();
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        return null;
    }

    /**
     * 验证用户登录信息
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String name = String.valueOf(token.getPrincipal());
        String password = new String((byte[]) token.getCredentials());
        User user = userService.login(name);
        return new SimpleAuthenticationInfo(user, user.getPassword(), getName());
    }
}
